﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace DARE
{
    /// <summary>
    /// [Not commented]
    /// </summary>
    [AttrTypeDependentSerializable(typeof(CMenuBarData))]
    public class CMenuBar: CWidget
    {
        #region fields
        
        private CButton m_button = null;
        private new bool m_isMoving;

        private Vector2 m_oldPos;
        private CStaticText m_text;

        #endregion

        #region properties

        public CStaticText Title { get { return m_text; } set { m_text = value; } }

        public CButton CButton
        {
            get { return m_button; }
            set
            {
                m_button = value;
                m_button.Size = new Vector2(0.1f, 0.1f);
                m_button.Position = new Vector2(0.8f, 0.8f);
            }
        }

        public override Vector2 Position
        {
            get
            {
                return base.Position;
            }
            set
            {
                base.Position = value;
                m_button.Position = new Vector2(m_rect.Width - 40, 5);
            }
        }

        public override Vector2 Size
        {
            get
            {
                return base.Size;
            }
            set
            {
                base.Size = value;
                m_text.Actualize();
                m_button.Position = new Vector2(m_rect.Width - 40, 5);
            }
        }

        public override Rectangle Rect
        {
            get
            {
                return base.Rect;
            }
            set
            {
                base.Rect = value;
                m_button.Position = new Vector2(m_rect.Width - 40, 5);
            }
        }

        public override void Actualize()
        {
            m_text.Rect = new Rectangle(m_rect.X, m_rect.Y, m_text.Rect.Width, m_text.Rect.Height);
            m_button.Position = new Vector2(m_rect.Width - 23, 3);
            base.Actualize();
        }

        #endregion

        #region event

        public void close(CWidget sender)
        {
            if (CDare.Instance.InputMgr.Mouse.ButtonReleasedEvent.First() == CMouse.EButton.LEFT)
            {
                if (this.m_parent != null)
                    m_parent.Hide();
                else
                    this.Hide();
            }
        }

        public void move(CWidget sender)
        {
            if (CDare.Instance.InputMgr.Mouse.ButtonPressedEvent.First() == CMouse.EButton.LEFT)
            {
                if (!m_isMoving)
                {
                    m_isMoving = true;
                    m_oldPos = new Vector2(Mouse.GetState().X, Mouse.GetState().Y);
                }
            }
        }

        public void stopMoving(CWidget sender)
        {
            if (CDare.Instance.InputMgr.Mouse.ButtonReleasedEvent.Count > 0 && CDare.Instance.InputMgr.Mouse.ButtonReleasedEvent.First() == CMouse.EButton.LEFT)
                m_isMoving = false;
        }

        #endregion

        #region ctor

        public CMenuBar(CHUDSystem hud, CWidget parent, string name)
            : base(hud, parent, name)
        {
            m_texture = HUD.Skinner.GetTexture("menubar");
            m_button = new CButton(hud, this, name + "_exitCButton");
            m_button.ReleasedTexture = HUD.Skinner.GetTexture("menubarbuttonreleased");
            m_button.ClickedTexture = HUD.Skinner.GetTexture("menubarbuttonclicked");

            m_button.Size = new Vector2(20f, 12f);
            m_button.Position = new Vector2(m_rect.Width - 23, 3);
            eventMethod release = close;
            m_button.OnRelease += release;
            m_isMoving = false;

            eventMethod click = move;
            OnClick += click;

            eventMethod releaseMenubar = stopMoving;
            OnRelease += releaseMenubar;

            m_text = new CStaticText(hud, this, name + "_title");
            m_text.Size = new Vector2(0.6f, 1);
            m_text.Text = "Here the title";
            m_text.Position = new Vector2(10, 2);
            m_text.Color = Color.White;
                       m_text.Actualize();
        }

        #endregion

        #region update

        public override bool Update(GameTime gameTime)
        {
            if (m_isMoving)
            {
                MouseState ms = Mouse.GetState();
                if (ms.LeftButton == ButtonState.Released)
                    stopMoving(this);
                else if (ms.X != m_oldPos.X || ms.Y != m_oldPos.Y)
                {
                    CWidget wid;
                    if (m_parent != null)
                        wid = this.m_parent;
                    else
                        wid = this;
                    Rectangle rect = wid.Rect;
                    Point pos = new Point(m_rect.Location.X + (int)(ms.X - m_oldPos.X), m_rect.Location.Y + (int)(ms.Y - m_oldPos.Y));
                    rect.Location = pos;
                    wid.Rect = rect;
                    m_oldPos.X = ms.X;
                    m_oldPos.Y = ms.Y;
                }
            }
            return base.Update(gameTime);
        }

        #endregion

        #region draw
        
        public override bool Draw(GameTime gameTime)
        {
            base.Draw(gameTime);
            return true;
        }

        #endregion

    }
}
